﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using SGLisp;
namespace GameUnit
{
    public class Actor
    {
        public Hashtable ActorTable;
        public GameObject ActorGameObject;
        Context ActorContext;
        Dictionary<string, List<IFunction>> EventFuncs; 
        public Unit Unit;
        public Actor(Hashtable actorTable)
        {
            this.ActorTable = actorTable;
            this.ActorContext = new Context(RT.MainContext);
            this.EventFuncs = new Dictionary<string, List<IFunction>>();
        }

        public void Show()
        {
            if (this.ActorGameObject == null)
            {
                GameObject ActorPrefab = (GameObject)Resources.Load((string)ActorTable[":prefab-path"]);
                this.ActorGameObject = (GameObject)GameObject.Instantiate(ActorPrefab);

                this.ActorGameObject.transform.parent = Unit.transform;
                this.ActorGameObject.transform.localPosition = ActorPrefab.transform.localPosition;
                this.ActorGameObject.transform.localRotation = ActorPrefab.transform.localRotation;
                this.ActorGameObject.transform.localScale = ActorPrefab.transform.localScale;
            }
            else
            {
                this.ActorGameObject.SetActive(true);
            }
        }
        public void AddActorEvent(string eventName,IFunction fn) 
        {
            if (!this.EventFuncs.ContainsKey(eventName)) 
            {
                this.EventFuncs.Add(eventName,new List<IFunction>());
            }
            this.EventFuncs[eventName].Add(fn);
        }
        public void OnLoad(Unit unit)
        {
            Unit = unit;
            this.ActorContext.Define("unit", new ObjectNode(this.Unit));
            this.ActorContext.Define("actor", new ObjectNode(this));
            Hashtable eventTable = (Hashtable)this.ActorTable[":unit-events"];
            foreach (DictionaryEntry eventItem in eventTable)
            {
                string eventName = (string)eventItem.Key;
                if (eventItem.Value is object[])
                {
                   object[] curEvents = (object[])eventItem.Value;
                   for (int i = 0; i < curEvents.Length;i++)
                   {
                       this.AddActorEvent(eventName,(IFunction)curEvents[i]);
                   }
                }
                else 
                {
                    this.AddActorEvent(eventName,(IFunction)eventItem.Value);
                }
                this.Unit.AddEventHandler(eventName,this.OnUnitEvent);
            }
        }
        public void OnUnitEvent(string eventName,string subMsg) 
        {
          List<ASTNode> Args =  new List<ASTNode>() { new StringNode(subMsg) };
          List<IFunction> curEvents = this.EventFuncs[eventName];
          for (int i = 0; i < curEvents.Count;i++)
          {
              curEvents[i].Exec(Args,this.ActorContext);
          }
        }

    }
}